<?php
/*
 *      _______ _     _       _     _____ __  __ ______
 *     |__   __| |   (_)     | |   / ____|  \/  |  ____|
 *        | |  | |__  _ _ __ | | _| |    | \  / | |__
 *        | |  | '_ \| | '_ \| |/ / |    | |\/| |  __|
 *        | |  | | | | | | | |   <| |____| |  | | |
 *        |_|  |_| |_|_|_| |_|_|\_\\_____|_|  |_|_|
 */
/*
 *     _________  ___  ___  ___  ________   ___  __    ________  _____ ______   ________
 *    |\___   ___\\  \|\  \|\  \|\   ___  \|\  \|\  \ |\   ____\|\   _ \  _   \|\  _____\
 *    \|___ \  \_\ \  \\\  \ \  \ \  \\ \  \ \  \/  /|\ \  \___|\ \  \\\__\ \  \ \  \__/
 *         \ \  \ \ \   __  \ \  \ \  \\ \  \ \   ___  \ \  \    \ \  \\|__| \  \ \   __\
 *          \ \  \ \ \  \ \  \ \  \ \  \\ \  \ \  \\ \  \ \  \____\ \  \    \ \  \ \  \_|
 *           \ \__\ \ \__\ \__\ \__\ \__\\ \__\ \__\\ \__\ \_______\ \__\    \ \__\ \__\
 *            \|__|  \|__|\|__|\|__|\|__| \|__|\|__| \|__|\|_______|\|__|     \|__|\|__|
 */
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Portal\Controller;
use Common\Controller\HomebaseController; 
/**
 * 首页
 */
class IndexController extends HomebaseController {

	function _initialize() {
		parent::_initialize();
		$this->vedios_model = D("Portal/Vedios");
		$this->guestbook_model=D("Common/Guestbook");
	}
    //首页
	public function index() {
		$this->_lists(array("vedio_status"=>array('eq',1)));
		$this->guestBook();
    	$this->display(":index");
    }

	public function guestBook(){
		$guestmsg = $this->guestbook_model->where(array("status"=>1))->limit(0,4)->order(array("createtime"=>"DESC"))->select();
		$this->assign("guestmsg",$guestmsg);
	}
	/**
	 * 文章列表处理方法,根据不同条件显示不同的列表
	 * @param array $where 查询条件
	 */
	private function _lists(){
		$term_id=I('request.term',0,'intval');

		$where['vedio_status']=array(array('eq',1),array('exp','IS NULL'),'OR');

		$this->vedios_model
			->alias("a")
			->where($where);

		$count=$this->vedios_model->count();

		$page = $this->page($count, 20);

		$this->vedios_model
			->alias("a")
			->join("__USERS__ c ON a.vedio_author = c.id")
			->where($where)
			->limit($page->firstRow , $page->listRows)
			->order("a.vedio_date DESC");
		if(empty($term_id)){
			$this->vedios_model->field('a.*,c.user_login,c.user_nicename');
		}else{
			$this->vedios_model->field('a.*,c.user_login,c.user_nicename,b.listorder,b.tid');
			$this->vedios_model->join("__TERM_RELATIONSHIPS__ b ON a.id = b.object_id");
		}
		$posts=$this->vedios_model->select();
		$vedioUrl[] = array();
		$vedioThumb[] = array();
		for($i=0;$i<count($posts);$i++){
			$vedioUrl[$i] = json_decode($posts[$i]['smeta'])->vedio;
			$vedioThumb[$i] = json_decode($posts[$i]['smeta'])->thumb;
		}
		for($j = 0; $j < count($vedioUrl);$j++){
			$this->assign("videoimage".$j,$vedioThumb[$j]);
			for($k = 0; $k < 3;$k++){
				$v1url = $vedioUrl[$j][$k]->url;
				if(strstr($v1url,"http") || strstr($v1url,"https")){
					$this->assign("http".$j,$v1url);
					continue;
				}
				if(substr(strrchr($v1url, '.'), 1) == "mp4"){
					$this->assign("vmp4".$j,sp_get_asset_upload_path($v1url));
				}
				if(substr(strrchr($v1url, '.'), 1) == "ogv"){
					$this->assign("vogv".$j,sp_get_asset_upload_path($v1url));
				}
				if(substr(strrchr($v1url, '.'), 1) == "webm"){
					$this->assign("vwebm".$j,sp_get_asset_upload_path($v1url));
				}
			}
		}
	}
}


